package com.ag.presentation.backEndBeans;

import com.ag.exceptions.ExceptionMessages;
import com.ag.presentation.businessDelegate.BusinessDelegatorView;
import com.ag.promanagement.Users;
import com.ag.utilities.DataSource;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import javax.faces.component.UIComponent;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

/**
 *
 * @author gahospin
 */
public class SesionView extends DataSource implements Serializable 
{
    private static final long serialVersionUID = -249479684581306551L;
    private String sbLogin;
    private String sbPassword;
    private boolean userSesion = false;
    private List<Users> usuarios;
    private boolean flag = true;
    private HttpSession session;
    private boolean miConfiguracion;
    private String param;
    private String actionFired;
    private String role;
    
    public SesionView() {
        super("");
    }

    public String getActionFired() {
            return actionFired;
    }
    
    public void setActionFired(String actionFired) {
            this.actionFired = actionFired;
    }
    
    public String getRole() {
                return role;
    }
    public void setRole(String role) {
            this.role = role;
    }
     public boolean  getMiConfiguracion() {
                return miConfiguracion;
    }
    public void setMiConfiguracion(boolean miConfiguracion) {
            this.miConfiguracion = miConfiguracion;
    }
    public String getSbLogin() {
            return sbLogin;
    }
    public void setSbLogin(String sbLogin) {
            this.sbLogin = sbLogin;
    }
    public String getSbPassword() {
            return sbPassword;
    }
    public void setSbPassword(String sbPassword) {
            this.sbPassword = sbPassword;
    }
    public String getParam() {
            return param;
    }
    public void setParam(String param) {
            this.param = param;
    }
    public String action_login()
    {
        String result="";

        if (sbLogin.equals(""))
        {
             FacesContext.getCurrentInstance()
                        .addMessage("",
                        new FacesMessage(ExceptionMessages.ENTITY_NULL));
          return result;
        }
        
        if (sbPassword.equals(""))
        {
             FacesContext.getCurrentInstance()
                        .addMessage("",
                        new FacesMessage(ExceptionMessages.ENTITY_NULL));
          return result;
        }

        try
        {
            String login=sbLogin;
            String pass=sbPassword;
            boolean bExiste=false;

            for(Users usuario:getUsuarios())
            {
                if(usuario.getLogin().equals(login))
                {
                    bExiste=true;
                    if(usuario.getPassword().equals(pass))
                    {
                        result="validate";
                        session=(HttpSession)FacesContext.getCurrentInstance().
                                getExternalContext().getSession(true);
                        session.setAttribute("login", login);
                        session.setAttribute("idusuario", usuario.getId());
                        session.setAttribute("rol", ""+usuario.getUserType().getId());
                        session.setAttribute("elbean", this);
                        setSession(session);
                        role=(String) session.getAttribute("rol");
                        userSesion=true;
                        validateMenu();
                        break;
                    }
                    else
                    {
                        FacesContext.getCurrentInstance()
                            .addMessage("",
                            new FacesMessage(/*ExceptionMessages.USUARIO_PASS*/"Usuario pass"));
                        break;
                    }
                }
                
            }

            if(!bExiste)
            {
                FacesContext.getCurrentInstance()
                        .addMessage("",
                        new FacesMessage(/*ExceptionMessages.USUARIO_NOEXISTE*/"usuario no existe"));
            }

        } catch (Exception e)
        {
            FacesContext.getCurrentInstance()
                        .addMessage("", new FacesMessage(e.getMessage()));
        }
        return result;
    }

     public void validateMenu()
    {
        if((""+session.getAttribute("idusuario")).equalsIgnoreCase("1"))
        {
            miConfiguracion=true;
        }
        else
        {
            miConfiguracion=false;
        }
    }

     public List<Users> getUsuarios()
     {
        if (flag)
        {
            try
            {
                usuarios = BusinessDelegatorView.getUsers();
                flag = false;
            } catch (Exception e)
            {
                flag = true;
                FacesContext.getCurrentInstance()
                            .addMessage("", new FacesMessage(e.getMessage()));
            }
        }

        return usuarios;
    }

        public String cerrarSesion(){

                //usuario = null;
                userSesion = false;
                return "exito";
        }
    public String action_close()
    {
        System.out.println("Cerrando session del usuario "+session.getAttribute("login"));
        if(session!=null)
        {
            session.invalidate();
            System.out.println("Session cerrada por usuario");
        }
        System.out.println("Session cerrada");

        return "close";

    }
        public boolean isUserSesion() {
                return userSesion;
        }
        public void setUserSesion(boolean userSesion) {
                this.userSesion = userSesion;
        }

     public void setSession(HttpSession session)
    {
        this.session=session;
    }

     public void primaryListener(ActionEvent e) {
        System.out.println("Entro al primaryListener");
                actionFired = ((UIComponent) e.getSource()).getClientId(FacesContext.getCurrentInstance());
                System.out.println("despues del actiofired");
        param = FaceUtils.getRequestParameter("option");
        System.out.println("param="+param);
                //valueChangeEffect.setFired(false);
        System.out.println("despues del valueChangeEffect");
                System.err.println("********Valor del parametro menu: "+param);

        }
    /*public void confMenu(ActionEvent e)
    {
                actionFired = ((UIComponent) e.getSource()).getClientId(FacesContext.getCurrentInstance());
                valueChangeEffect.setFired(false);
        }*/

        public String navegacion(){
        System.out.println("en navegacion con el param ");
                return param;
        }

	@Override
	protected boolean isDefaultAscending(String sortColumn) {
		// TODO Auto-generated method stub
		return false;
	}
}
